//
//  BBSListTableViewCell.m
//  tianyaQing
//
//  Created by Rocket on 12-3-9.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "BBSListTableViewCell.h"
#import "UIColor+HTML.h"
#import "NoteInfo+YL8G.h"

@implementation BBSListTableViewCell

@synthesize authorNameLabel=authorNameLabel_;
@synthesize createTimeLabel=createTimeLabel_;

+ (int)heightForTableViewCell
{
    return 74;    
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    {
		self.textLabel.font = [UIFont systemFontOfSize:16.0f];
		self.textLabel.numberOfLines = 2;
		self.detailTextLabel.font = [UIFont systemFontOfSize:14.0f];
		self.detailTextLabel.numberOfLines =2;
		self.accessoryType = UITableViewCellAccessoryNone;
		
		authorNameLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 20, 100, 20)];
		authorNameLabel_.backgroundColor = [UIColor clearColor];
		authorNameLabel_.font = [UIFont systemFontOfSize:12.0f];
		authorNameLabel_.textColor = [UIColor colorWithHTMLName:@"#667387"];
		[self.contentView addSubview:authorNameLabel_];
		[authorNameLabel_ release];
        
		createTimeLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 20, 100, 20)];
		createTimeLabel_.backgroundColor = [UIColor clearColor];
		createTimeLabel_.font = [UIFont systemFontOfSize:12.0f];
		createTimeLabel_.textColor = [UIColor colorWithHTMLName:@"#667387"];
		[self.contentView addSubview:createTimeLabel_];
		[createTimeLabel_ release];
        
        hotBtn_ = [UIButton buttonWithType:UIButtonTypeCustom];
        [hotBtn_ setTitleColor:[UIColor colorWithHTMLName:@"#667387"] forState:UIControlStateNormal];
        [hotBtn_ setImage:[UIImage imageNamed:@"hot_icon.png"] forState:UIControlStateNormal];
		hotBtn_.userInteractionEnabled = FALSE;
        hotBtn_.imageEdgeInsets=UIEdgeInsetsMake(0,-5,0,0);
		hotBtn_.frame = CGRectMake(20, 20, 100, 20);
		hotBtn_.titleLabel.font = [UIFont systemFontOfSize:12];
		[self.contentView addSubview:hotBtn_];
    }
    return self;
}

- (void)initDataWithNoteInfo:(NoteInfo*)info
{
    info_=info;
    self.textLabel.text=info.title;
    authorNameLabel_.text=info.author;
    if ([info.createTime length]>10)
    {
        createTimeLabel_.text=[info.createTime substringToIndex:10];
        //NSRange range=NSMakeRange(5,11);
        //createTimeLabel_.text=[info.createTime substringWithRange:range];
    }
    else
    {
        createTimeLabel_.text=info.createTime;
    }
    [hotBtn_ setTitle:[NSString stringWithFormat:@"%d",info.clickCount+info.replyCount] forState:UIControlStateNormal];
}

- (void) layoutSubviews 
{
	[super layoutSubviews];
	int xOffset = 15;
	int yOffset = self.frame.size.height - 20;
	if (self.imageView.image) 
    {
		xOffset += [self.imageView.image size].width;
	}
    CGRect frame=self.textLabel.frame;
    frame.origin.y-=5;
    self.textLabel.frame=frame;
    
	if ([authorNameLabel_.text length] > 0) 
    {
		//authorNameLabel_.frame = CGRectMake(xOffset, yOffset-3, 70, 20);
        authorNameLabel_.frame = CGRectMake(self.textLabel.frame.origin.x, yOffset-3, 90, 20);
		xOffset += 75;
	}
    if ([createTimeLabel_.text length] > 0) 
    {
        createTimeLabel_.hidden=FALSE;
		createTimeLabel_.frame = CGRectMake(100, yOffset-3, 90, 20);
	}
    else
    {
        createTimeLabel_.hidden=TRUE;
    }


    int width=15;
    NSString* displayStr=[NSString stringWithFormat:@"%d",info_.clickCount+info_.replyCount];
    width+=[displayStr sizeWithFont:hotBtn_.titleLabel.font].width;
    width+=15;
    hotBtn_.frame = CGRectMake(self.frame.size.width-width, self.frame.size.height-25, 20+[displayStr sizeWithFont:hotBtn_.titleLabel.font].width, 25);
    [self changeTextColorIfReaded];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{
    [super setSelected:selected animated:animated];
}

- (void)changeTextColorIfReaded
{
    if ([info_ isExistInDatabase])
    {
        self.textLabel.textColor = [UIColor colorWithHTMLName:@"#7d7d7d"];;
		self.detailTextLabel.textColor = [UIColor colorWithHTMLName:@"#7d7d7d"];
        authorNameLabel_.textColor = [UIColor colorWithHTMLName:@"#7d7d7d"];
        createTimeLabel_.textColor = [UIColor colorWithHTMLName:@"#7d7d7d"];
    }
}

- (void)dealloc 
{
    [super dealloc];
}

@end
